/**
 * 
 */
package co.edu.eam.iso.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import co.edu.eam.iso.logger.Logger;

/**
 * @author Giovanny Ramirez Blandon
 * @version 1.0
 */
public class DataSource {

	private static final String DATABASE_FOLDER = "database";
	
	private static final String DATABASE_NAME = "evaluate";
	
	private static final String DATABASE_URL = "jdbc:sqlite:";
	
	private static Connection connection;
	
	static {
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (ClassNotFoundException e) {
			Logger.getInstance().registrarLog(e);
		}		
	}
	
	/**
	 * @return the connection
	 */
	public static Connection getConnection() {
		try {
			connection = DriverManager.getConnection(getUrl());
		} catch (Exception ex) {
			Logger.getInstance().registrarLog(ex);
		}

		return connection;
	}
	
	public static void closeConnection() {
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			Logger.getInstance().registrarLog(e);
		}
	}
	
	private static String getUrl() {
		StringBuilder url = new StringBuilder();
		url.append(DATABASE_URL);
		url.append(System.getProperty("user.dir"));
		url.append(System.getProperty("file.separator"));
		url.append(DATABASE_FOLDER);
		url.append(System.getProperty("file.separator"));
		url.append(DATABASE_NAME);

		return url.toString();
	}
}
